seajs.use(['zepto', 'calendar', 'dialog', 'verify-set', 'manager', 'ver-error', 'verify-code'], function ($, Calendar, dialog, verify, Manager, verError, verifyCode) {
    var urlSplit = "/onlineBook/onlineBookGT/stepOne/params/eyJyb3V0ZUlkIjoyMDM1Nzc0MiwiYmVnaW5EYXRlIjoiMjAxNS0wOS0xMCIsImFkdWx0TnVtIjoyLCJjaGlsZE51bSI6MCwiYm9va0lkIjoiIiwicHJvZHVjdFR5cGUiOjEsInRvdGFsUHJpY2UiOjEyNjE4fQ==".split("/"), manager = new Manager(), urlParam = JSON.parse(manager.decode(urlSplit[urlSplit.length - 1])), yearTimeNum = 31536e6, noAdultNum = 567648e6, allNoAdult = false, isSixty = false, isEighty = false, isLogin = false, isNewUser = false, isConfirmation = true, specialPeopleInfo = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0};
    var confirm_Flag = false;
    if (userId != 0) {
        isLogin = true;
    }
    function setWebQrScanP(productId) {
        if (productId > 0) {
            var hash = decodeURIComponent(window.location.hash.replace(/^#/, ''));
            var result = /qrp=(\d+)/.exec(hash), p;
            if (result && result.length >= 2) {
                p = result[1];
                if (p > 0) {
                    var str = tuniu_common.base64.encode(productId + '_' + p);
                    document.cookie = 'qr_p=' + str + ';expires=' + new Date(new Date().getTime() + 60 * 20 * 1000).toGMTString() + ';domain=.tuniu.com;path=/';
                }
            }
        }
    };
    setWebQrScanP(urlParam.routeId);
    function forceRefresh() {
        var initFlag = false;
        $(window).bind('popstate', function (e) {
            if (initFlag) {
                window.location.reload();
            } else {
                initFlag = true;
            }
        });
    }

    forceRefresh();
    if (typeof isAllNoAdultLength != "undefined" && isAllNoAdultLength > 0) {
        var isAllNoAdults = [];
        for (var i = 0; i < isAllNoAdultLength; i++) {
            isAllNoAdults.push({noAdult: null, value: "", sixty: false, eighty: false, type: 1});
        }
    }
    var this_ajax_url = '/promotion/miaoshaV2';
    var tempDataArray = [], tempData = {}, checkAjax = "";
    verifyCode.changeImg($('#imgTelObj'), $('#imgSrc'));
    $(".form-item select").on("change", function (e) {
        var select = $(e.target);
        var splitByName = select.attr("name").split("_");
        var index = splitByName[splitByName.length - 1];
        var formItem = select.parents(".tourist-content");
        $(".other", formItem).toggle(parseInt(select.val()));
        if (select.val() == 0) {
            $(".other input", formItem).val("");
            $(".other i", formItem).attr("class", "icon-radiobox-unchecked");
        } else {
            $(".other input", formItem).val("");
        }
        isAllNoAdults[index].noAdult = null;
        isAllNoAdults[index].value = "";
        isAllNoAdults[index].sixty = false;
        isAllNoAdults[index].eighty = false;
        isAllNoAdults[index].type = select.val();
        nisNAdult();
        clearTouristPspt($("input[name*='c1_tourist_paperNum']", formItem));
    });
    var checked = ["icon-radiobox-unchecked", "icon-radiobox-checked"];
    $(".form-input label").on("click", function (e) {
        var i = $(this).find("i");
        var parent = $(this).parents(".form-input");
        if ($(this).index() == 0) {
            if (i.hasClass(checked[0])) {
                i.removeClass(checked[0]);
                i.addClass(checked[1]);
                parent.find("i").eq($(this).index() + 1).removeAttr("class").addClass(checked[0]);
            }
        }
        if ($(this).index() == 1) {
            if (i.hasClass(checked[0])) {
                i.removeClass(checked[0]);
                i.addClass(checked[1]);
                parent.find("i").eq($(this).index() - 1).removeAttr("class").addClass(checked[0]);
            }
        }
    });
    $("input[type='date']").live("change", function (e) {
        var ele = $(e.target);
        var splitByName = ele.attr("name").split("_");
        var index = splitByName[splitByName.length - 1];
        var value = $(e.target).val();
        nisNAdult(value, index, value, 0);
        isSpecialPeopleInfoShow();
    });
    function nisNAdult(date, index, value, type) {
        if (date) {
            var nowLongDate = new Date().getTime();
            var birthdayLongDate = new Date(date).getTime();
            isAllNoAdults[index].value = value;
            isAllNoAdults[index].type = type;
            var age = parseInt((nowLongDate - birthdayLongDate) / yearTimeNum);
            if (typeof healthLimitMax != "undefined") {
                if (age < healthLimitMax && age >= healthLimitMin) {
                    isSixty = true;
                    isAllNoAdults[index].sixty = true;
                }
            }
            if (typeof ageLimitMax != "undefined") {
                if (age >= ageLimitMax) {
                    isAllNoAdults[index].eighty = true;
                    isEighty = true;
                }
            }
            if ((nowLongDate - birthdayLongDate) > noAdultNum) {
                isAllNoAdults[index].noAdult = true;
            } else {
                isAllNoAdults[index].noAdult = false;
            }
            isShow();
        } else {
            isShow();
        }
        function isShow() {
            var ele = $(".specialPeopleInfo");
            var noAdultflag = false, valFlag = false, ageFlag = false, typeFlag = false;
            for (var i = isAllNoAdults.length - 1; i >= 0; i--) {
                if (isAllNoAdults[i].value.length == 0) {
                    ageFlag = true;
                }
                if (isAllNoAdults[i].type != 1) {
                    typeFlag = true;
                }
            }
            if (!typeFlag && !ageFlag) {
                ele.hide();
                return;
            } else {
                ele.show();
            }
            $(".age").show();
            if (!ageFlag) {
                $(".age").hide();
                isSpecialPeopleInfoShow();
            }
            for (var i = isAllNoAdults.length - 1; i >= 0; i--) {
                if (isAllNoAdults[i].noAdult) {
                    ele.show();
                    $("#isNoAdults").hide();
                    return;
                } else {
                    noAdultflag = true;
                    if (isAllNoAdults[i].value.length == 0) {
                        valFlag = true;
                    }
                }
            }
            if (noAdultflag && !valFlag) {
                allNoAdult = true;
                ele.hide();
            }
            if (noAdultflag && valFlag) {
                $(".specialPeopleInfo").show();
                $(".form-item:last", ele).show();
            }
        }
    }

    $(".tourist-title span").click(function (e) {
        var span = $(e.target);
        var parent = span.parent().next();
        var realname = $("#c0_tourist input[name='realname']").val();
        var tel = $("#c0_tourist input[name='tel']").val();
        $("input[name*='name']", parent).val(realname);
        $("input[name*='phoneNum']", parent).val(tel);
    });
    $("input[name='tel']").keyup(function (e) {
        var ele = $(e.target);
        var val = ele.val();
        var length = ele.val().length;
        $('.need-captcha').css('display', 'none');
        var isMemberAjax = $("#isMemberAjax").val().replace("/onlineBook", "");
        if (verify.tel.test(val)) {
            if (!isLogin) {
                $.ajax({url: isMemberAjax + '?data={"tel": "' + val + '"}', success: function (data) {
                    data = JSON.parse(data);
                    console.log('isMemberAjax', data);
                    if (data.success === true && data.data.isMember == "true") {
                        $(".againTel").hide();
                        if (data.data.needCaptcha) {
                            $('[name="needCaptcha"]').val(1);
                        }
                        isNewUser = false;
                        getLoginStatus(function () {
                        }, val);
                    }
                    if (data.success === true && data.data.isMember == "false") {
                        $(".password").hide();
                        $('.need-captcha').attr('style', 'display: none');
                        $(".againTel").show();
                        $("#check_form").show();
                        $("input[name='againTel']").focus();
                        isNewUser = true;
                    }
                }})
            }
        } else {
            $(".password").hide();
            $('.need-captcha').attr('style', 'display: none');
            $(".againTel").hide();
        }
    });
    $('#imgTelObj').on('click', function (e) {
        e.preventDefault();
        verifyCode.changeImg($('#imgTelObj'), $('#imgSrc'));
    });
    $("#confirmation").on("click", function (e) {
        var ele = $(e.target);
        if (ele.attr("class") == "icon-checkbox-unchecked") {
            ele.removeAttr("class").attr("class", "icon-checkbox-checked");
            isConfirmation = true;
        } else {
            ele.removeAttr("class").attr("class", "icon-checkbox-unchecked");
            isConfirmation = false;
        }
    });
    $(".form-item input[name*='paperNum']").keyup(function (e) {
        var ele = $(e.target);
        var val = ele.val();
        var length = ele.val().length;
        var splitByName = ele.attr("name").split("_");
        var index = splitByName[splitByName.length - 1];
        var paperType = $("select[name='c1_tourist_paperType_" + index + "']").val();
        var birthday = $("input[name='c1_tourist_birthday_" + index + "']");
        var sexSelect = $("div[name='c1_tourist_sex_" + index + "']");
        if (paperType == 1) {
            if (length == 15 || length == 18) {
                var flag = verify.card.isIdCard(val);
                if (flag) {
                    var brith = verify.card.getBrith(val, length);
                    if (brith.isValidityBrith) {
                        var sex = verify.card.getSex(val);
                        $("#c1_tourist_" + index).find(".form-input label:eq(" + (sex == 1 ? 0 : 1) + ")").click();
                        birthday.val(brith.date);
                        nisNAdult(brith.date, index, val, 1);
                    }
                }
            } else {
                isAllNoAdults[index].noAdult = null;
                isAllNoAdults[index].value = "";
                isAllNoAdults[index].sixty = false;
                isAllNoAdults[index].eighty = false;
                isAllNoAdults[index].type = 1;
                nisNAdult(new Date(), index, val, 1);
            }
        }
        isSpecialPeopleInfoShow();
    });
    $("#submit_c0").click(function () {
        var self = $(this);
        var realname = $("#c0_tourist input[name='realname']");
        var tel = $("#c0_tourist input[name='tel']");
        var email = $("#c0_tourist input[name='email']");
        var captcha = $("#captcha");
        var yzmFlag = $("#yzmFlag");
        if (realname.val().length == 0) {
            return fieldVerify(realname, "请填写您的姓名", function () {
                return $(this).val().length > 0
            });
        }
        if (tel.val().length == 0 || !verify.tel.test(tel.val())) {
            return fieldVerify(tel, "请正确填写您的手机号码", function () {
                return $(this).val().length > 0 && verify.tel.test($(this).val())
            });
        }
        if (yzmFlag.val() == 1 && confirm_Flag == false) {
            return fieldVerify(captcha, "请输入正确的校验码", function () {
                return yzmFlag.val() != 1 || confirm_Flag
            });
        }
        if (email.val().length != 0) {
            if (!verify.email.test(email.val())) {
                return fieldVerify(email, "请正确填写您的邮箱地址", function () {
                    return verify.email.test(email.val())
                });
            }
        }
        var submitData = {routeId: urlParam.routeId, beginDate: urlParam.beginDate, adultNum: urlParam.adultNum, childNum: urlParam.childNum, bookId: bookId, validCode: captcha.val(), canBookOnlineFlag: canBookOnlineFlag, contactInfo: {realname: realname.val(), tel: tel.val(), email: email.val()}};
        getLoginStatus(function () {
            setDuplicateOrderData({productType: 1, productId: urlParam.routeId, bookId: bookId, submitData: manager.encode(JSON.stringify(submitData))}, function () {
                submitData = manager.encode(JSON.stringify(submitData));
                $("#submitData").val(submitData);
                self.html("处理中...").unbind("click").addClass("unactive");
                $("#form").submit();
            })
        }, tel.val());
    });
    $("#submit_c1").click(function () {
        var self = $(this);
        var realname = $("#c0_tourist input[name='realname']");
        var tel = $("#c0_tourist input[name='tel']");
        var email = $("#c0_tourist input[name='email']");
        var captcha = $("#captcha");
        var yzmFlag = $("#yzmFlag");
        if (realname.val().length == 0 || realname.val().length < 2) {
            if (realname.val().length == 0) {
                var str = '请填写您的姓名';
            } else {
                var str = '请正确填写您的联系人姓名';
            }
            return fieldVerify(realname, str, function () {
                if ($(this).val().length == 0) {
                    fieldVerify($(this), "请填写您的姓名", false);
                    return false;
                } else if ($(this).val().length < 2) {
                    fieldVerify($(this), "请正确填写您的联系人姓名", false);
                    return false;
                } else {
                    return true;
                }
            });
        } else {
            fieldVerify(realname);
        }
        if (tel.val().length == 0 || !verify.tel.test(tel.val())) {
            tel.focus();
            return fieldVerify(tel, "请正确填写您的手机号码", function () {
                return $(this).val().length > 0 && verify.tel.test($(this).val())
            });
        }
        if (yzmFlag.val() == 1 && confirm_Flag == false) {
            return fieldVerify(captcha, "请输入正确的校验码", function () {
                return yzmFlag.val() != 1 || confirm_Flag
            });
        }
        if (email.val().length == 0 || !verify.email.test(email.val())) {
            email.focus();
            return fieldVerify(email, "请正确填写您的邮箱地址", function () {
                return verify.email.test(email.val())
            });
        }
        var isCorrect = checkAllNames();
        if (!isCorrect) {
            return;
        }
        if (!checkTouristPspt())return false;
        var year, yearFlag = true;
        $('.c1_birthday:visible').each(function (j, m) {
            year = $(m).val().split("-")[0];
            if (year < 1900 || year > 2099) {
                dialog({type: 'info', align: 'center', message: '请您正确填写出生日期'});
                $(m).focus();
                yearFlag = false;
                return false;
            }
        });
        if (!yearFlag)return false;
        var telLengthFlag = false;
        $('.c1_tel').each(function (k, l) {
            if ($(l).val()) {
                telLengthFlag = true;
                return false;
            }
        });
        if ($('.c1_tel').length && !telLengthFlag) {
            dialog({type: 'info', align: 'center', message: "请您至少填写一位出游人的手机号码"});
            $("input[name='c1_tourist_phoneNum_0']").focus();
            return false
        }
        var touristList = [];
        var phoneFlag = false;
        $("#touristList .tourist-content").each(function (index, item) {
            var touristData = {"name": "", "sex": 9, "paper_type": 1, "paper_num": "", "tel": "", "birthday": "", "paper_deadline": "0000-00－00", "nation": "", "firstname": "", "lastname": "", "gatxz_csd": "", "gatxz_qfd": "", "passport_qfd": "", "has_xgqz": 0, "xgqz_type": 0, "xgqz_deadline": "0000-00-00", "has_amqz": 0, "amqz_type": 0, "amqz_deadline": "0000-00-00", "remark": "", "visa_type": ""};
            $(".form-input input,.form-input select", item).each(function (i, input) {
                var name = $(input).attr("class");
                if (name) {
                    name = name.replace("c1_", "");
                    touristData[name] = $(input).val();
                    if (name == "phoneNum") {
                        if ($(input).val().length == 0) {
                            phoneFlag = true;
                            $(input).css({border: "1px solid red"});
                        }
                    }
                }
            });
            var index = $(".form-input label i.icon-radiobox-checked", item).parent().index();
            touristData.sex = index == 0 ? 1 : 0;
            touristList.push(touristData);
        });
        if ($(".specialPeopleInfo").css('display') != 'none') {
            var count = 0;
            var length = 0;
            $(".specialPeopleInfo .form-item").each(function (index, item) {
                if ($(item).css('display') != 'none') {
                    length++;
                    count += $(item).find("i.icon-radiobox-checked").length;
                    if ($(item).find("i.icon-radiobox-checked").length == 0) {
                        fieldVerify($(item), "请核实您的出游人信息", false);
                    } else {
                        fieldVerify($(item));
                    }
                }
            });
            if (count < length) {
                return;
            }
        }
        var submitData = {routeId: urlParam.routeId, beginDate: urlParam.beginDate, adultNum: urlParam.adultNum, childNum: urlParam.childNum, bookId: bookId, validCode: captcha.val(), canBookOnlineFlag: canBookOnlineFlag, contactInfo: {realname: realname.val(), tel: tel.val(), email: email.val()}, touristInfo: touristList, specialPeopleInfo: specialPeopleInfo};
        var type_1 = false;
        var type_2 = false;
        var type_3 = false;
        var type_4 = false;
        var birthday = false;
        var telLength = false;
        var paper_num = false;
        var nameLength = false;
        var idCard = false;
        var nameEmptyIndex = -1;
        for (var i in touristList) {
            if (touristList[i].birthday.length > 0) {
                var age = parseInt((new Date().getTime() - new Date(touristList[i].birthday).getTime()) / yearTimeNum);
                if (age > 18) {
                    allNoAdult = false;
                }
                if (typeof healthLimitMax != "undefined") {
                    if (age <= healthLimitMax && age >= healthLimitMin) {
                        type_3 = true;
                    }
                }
                if (typeof ageLimitMax != "undefined") {
                    if (age >= ageLimitMax) {
                        type_1 = true;
                    }
                }
                if (typeof ageLimitMin != "undefined") {
                    if (age < ageLimitMin) {
                        type_2 = true;
                    }
                }
                if (typeof onlineLimitMax != "undefined") {
                    if (age <= onlineLimitMax && age >= onlineLimitMin) {
                        type_4 = true;
                    }
                }
            }
            if (touristList[i].tel.length > 0) {
                telLength = true;
            }
            if (touristList[i].name.length < 2) {
                nameLength = true;
                nameEmptyIndex = i;
            }
            if (touristList[i - 1]) {
                if (touristList[i].paper_num == touristList[i - 1].paper_num && touristList[i].paper_num.length > 0 && touristList[i - 1].paper_num.length > 0) {
                    paper_num = true;
                }
            }
            if (parseInt(touristList[i]["paper_type"]) == 1 && touristList[i]["paper_num"].length != 0) {
                var flag = verify.card.isIdCard(touristList[i]["paper_num"]);
                if (!flag) {
                    idCard = true;
                }
            }
        }
        if (type_1) {
            submitData.specialPeopleInfo["1"] = 1;
        }
        if (type_2) {
            submitData.specialPeopleInfo["2"] = 1;
        }
        if (type_3) {
            submitData.specialPeopleInfo["3"] = 1;
        }
        if (type_4) {
            submitData.specialPeopleInfo["4"] = 1;
        }
        if (allNoAdult) {
            submitData.specialPeopleInfo["6"] = 1;
        } else {
            submitData.specialPeopleInfo["6"] = 0;
        }
        getLoginStatus(function () {
            setDuplicateOrderData({productType: 1, productId: urlParam.routeId, bookId: bookId, submitData: manager.encode(JSON.stringify(submitData))}, function () {
                submitData = manager.encode(JSON.stringify(submitData));
                $("#submitData").val(submitData);
                self.html("处理中...").unbind("click").addClass("unactive");
                $("#form").submit();
            })
        }, tel.val());
    });
    function checkTouristPspt() {
        var n;
        var nValue = '';
        var nPsptType = 0;
        var idFlag = true;
        var tempData = [];
        $('#touristList .tourist-content').each(function (i, item) {
            n = $(item).find('.c1_paper_num');
            nValue = n.val() || '';
            nPsptType = $(item).find('.c1_paper_type').val();
            if (n.length > 0) {
                if (nValue.length == 0) {
                    if (typeof(credentialsIdFlag) !== 'undefined' && credentialsIdFlag) {
                        fieldVerify(n, '请填写所有出游人的证件号', false);
                        idFlag = false;
                        return false;
                    } else {
                        fieldVerify(n);
                    }
                } else if (nPsptType == 1 && !verify.card.isIdCard(nValue)) {
                    fieldVerify(n, '请正确填写您的身份证号码', false);
                    idFlag = false;
                    return false;
                } else {
                    if (tempData.indexOf(nValue) > -1) {
                        fieldVerify(n, '您填写的证件号码不能重复', false);
                        idFlag = false;
                        return false;
                    } else {
                        tempData.push(n.val());
                        fieldVerify(n);
                    }
                }
            }
        });
        return idFlag;
    }

    function clearTouristPspt(el) {
        if (el && el.length) {
            fieldVerify(el);
        } else {
            $('#touristList .c1_paper_num').each(function (i, item) {
                fieldVerify($(item));
            });
        }
    }

    function isSpecialPeopleInfoShow() {
        var flag = false;
        $(".specialPeopleInfo .form-item").each(function (index, item) {
            if ($(item).css('display') != 'none') {
                flag = true;
            }
        });
        if (!flag) {
            $(".specialPeopleInfo").hide();
        }
        return flag;
    }

    function fieldVerify(ele, msg, isTrue) {
        if (ele && ele[0].nodeName == 'INPUT' && msg) {
            if (ele.hasClass('form-item') || ele.parents('.form-item').length == 0) {
                verError.act(ele, msg, ele);
            } else {
                verError.act(ele.parents('.form-item').eq(0), msg, ele);
            }
        } else {
            if (ele.hasClass('form-item') || ele.parents('.form-item').length == 0) {
                verError.act(ele, msg);
            } else {
                verError.act(ele.parents('.form-item').eq(0), msg);
            }
        }
        if (isTrue !== false) {
            ele.unbind("change").bind("change", function (e) {
                if (typeof(isTrue) === 'function' ? isTrue.call(this) : $(this).val().length != 0) {
                    $(this).unbind('change');
                    verError.act($(this).parents('.form-item').eq(0));
                }
            });
        }
        return false;
    }

    function setDuplicateOrderData(obj, fn) {
        gotoCheck(obj, fn);
    }

    function getLoginStatus(fn, tel, again) {
        if (again) {
            fn();
            return;
        }
        if (isLogin) {
            if ($.isFunction(fn)) {
                fn();
            }
        } else {
            if (!isNewUser) {
                fieldVerify($('.password input[type=\"password\"]'), '请输入您的密码进行登录', false);
                $(".password").show();
                if ($('[name="needCaptcha"]').val() == 1) {
                    $('.need-captcha').removeAttr('style');
                } else {
                    $('.need-captcha').attr('style', 'display:none');
                    $('#veryPicCode').val('');
                }
                $(".againTel").hide();
                $(".login").unbind("click").bind("click", function () {
                    var password = $(".password input[type=\"password\"]").val();
                    var tel = $("#c0_tourist input[name='tel']").val();
                    var captcha = $('#veryPicCode').val() || '';
                    if (password.length == 0) {
                        fieldVerify($('.password input[type=\"password\"]'), '请输入您的密码', false);
                        return;
                    }
                    if (($('[name="needCaptcha"]').val() == 1) && captcha.length == 0) {
                        fieldVerify($('#veryPicCode'), '请输入您的验证码', false);
                        return;
                    }
                    var loginData = {'username': tel, 'password': password, 'captcha': captcha, 'remember': 1};
                    $.ajax({type: 'POST', dataType: 'json', data: {data: JSON.stringify(loginData)}, url: $('#doLoginAjax').val(), success: function (json) {
                        console.log('doLoginAjax', doLoginAjax);
                        if (!json.success) {
                            dialog({type: 'info', align: 'center', message: json.data.msg});
                            verifyCode.changeImg($('#imgTelObj'), $('#imgSrc'));
                            $.ajax({type: 'get', dataType: 'json', url: $('#captchaAjax').val(), success: function (json) {
                                console.log('captcha', json);
                                if (json.success) {
                                    if (json.data.needCaptcha) {
                                        $('[name="needCaptcha"]').val(1);
                                        $('.need-captcha').removeAttr('style');
                                    } else {
                                        $('.need-captcha').attr('style', 'display: none');
                                        $('#veryPicCode').val('');
                                    }
                                } else {
                                    dialog({type: 'info', align: 'center', message: json.msg});
                                }
                            }});
                        } else {
                            $(".password").hide();
                            $('.need-captcha').attr('style', 'display: none');
                            $('[name="password"]').val('');
                            $('#veryPicCode').val('');
                            getLoginStatus(function () {
                                dialog({type: 'info', align: 'center', message: "登录成功"});
                                isLogin = true;
                                $("#resend").trigger("click");
                                $("#check_form").removeClass("hide");
                            }, tel, true)
                        }
                    }});
                });
            } else {
                if (!isLogin && isConfirmation) {
                    if ($.isFunction(fn)) {
                        fn();
                    }
                } else {
                    fieldVerify($('.againTel'), '请您接受并同意途牛会员协议', false);
                    return false;
                }
            }
        }
    }

    function gotoCheck(checkdata, fn) {
        var addOrderFail = $('#addOrderFail').val();
        try {
            $.ajax({url: "../../checkDuplicate/" + checkdata.productId + "/" + bookId, success: function (data) {
                data = JSON.parse(data);
                if (data.success) {
                    if (data.isDuplicate) {
                        $("#checkSubmitData").val(checkdata.submitData);
                        $("#productType").val(checkdata.productType);
                        $("#productId").val(checkdata.productId);
                        $("#checkForm").submit();
                    } else {
                        if ($.isFunction(fn)) {
                            fn();
                        }
                    }
                } else {
                    if (!isNewUser) {
                        window.location = addOrderFail + "?" + 'errorMsg=' + data.msg;
                    } else {
                        if ($.isFunction(fn)) {
                            fn();
                        }
                    }
                }
            }})
        } catch (e) {
            window.location = addOrderFail;
        }
    }

    $(".specialPeopleInfo i").click(function (e) {
        var ele = $(e.target);
        var labelIndex = ele.parent().index();
        var value = ele.parents(".form-item").attr("type");
        var id = ele.parents(".form-item").attr("id");
        if (labelIndex == 0) {
            specialPeopleInfo[value] = 1;
        }
        if (labelIndex == 1) {
            specialPeopleInfo[value] = 0;
        }
        if (id == "isNoAdults") {
            if (labelIndex == 0) {
                allNoAdult = true;
            } else {
                allNoAdult = false;
            }
        }
    });
    if (typeof childNum == "undefined" || childNum == 0) {
        $(".select-info").css({"line-height": "87px"});
    }
    $("#forget").off('click').on('click', function (e) {
        e.preventDefault();
        callVerificationDialog($('input[name="tel"]').val(), $('#imgTelObj'));
    });
    if (!isLogin) {
        $("#check_form").addClass("hide");
    }
    function checkCellPhone() {
        if (isLogin && !isNewUser) {
            if ($("[name='tel']").val().length != 11) {
                $("#check_form").addClass("hide");
            } else {
                if (!confirm_Flag) {
                    $("#check_form").removeClass("hide");
                }
            }
        }
    }

    checkCellPhone();
    $("#userPhone").on('input', checkCellPhone);
    $("#resend").click(function () {
        if (sessionStorage.cacheTime && (parseInt(sessionStorage.cacheTime, 10) + 10 * 60 * 1000) < new Date().getTime()) {
            sessionStorage.sendTimes = 0;
            sessionStorage.cacheTime = new Date().getTime();
        }
        if (!sessionStorage.sendTimes) {
            sessionStorage.sendTimes = 0;
        }
        if (parseInt(sessionStorage.sendTimes, 10) < 5) {
            if ($("#resend").hasClass("active")) {
                $("#freeMsg").addClass("hide");
                $("#confirm_info").removeClass("hide");
                $("#reget").addClass('hide');
                sendMsg();
                if (parseInt(sessionStorage.sendTimes, 10) == 4) {
                    sessionStorage.cacheTime = new Date().getTime();
                    sessionStorage.sendTimes = 10;
                }
            } else {
                confirmMsg();
            }
        } else if (sessionStorage.sendTimes) {
            dialog({type: 'info', align: 'center', message: "您已达到校验码重试上限，请10分钟后再试"});
        }
    });
    function recouting() {
        if (window.recoutingTimer) {
            clearInterval(window.recoutingTimer);
        }
        $('#resend').removeClass('active');
        $('#reget').addClass('hide');
        $('#countDown').removeClass('hide');
        var countNum = 60;
        $('#countDown').html('(60s)');
        window.recoutingTimer = setInterval(function () {
            $('#countDown').html('(' + --countNum + 's)');
            if (countNum <= 0) {
                clearInterval(window.recoutingTimer);
                $('#countDown').html('');
                $('#confirm_info').addClass('hide');
                $('#reget').removeClass('hide');
                $('#resend').addClass('active');
            }
        }, 1000);
    }

    function sendMsg() {
        recouting();
        var sendTelCodeAjax = $("#sendTelCodeAjax").val();
        var userPhone = $("#userPhone").val();
        $.ajax({type: 'POST', dataType: 'json', url: sendTelCodeAjax, data: {data: '{"telNum":"' + userPhone + '"}'}, success: function (data) {
            if (data.success) {
                dialog({type: 'info', align: 'center', message: "校验码已发送至您的手机，请查收"});
                sessionStorage.sendTimes = parseInt(sessionStorage.sendTimes, 10) + 1;
            }
        }});
    }

    function confirmMsg() {
        var sendCaptchaAjax = $("#sendCaptchaAjax").val();
        var userPhone = $("#userPhone").val();
        var captcha = $("#captcha").val();
        $.ajax({type: 'POST', dataType: 'json', url: sendCaptchaAjax, data: {data: '{"telNum":"' + userPhone + '","code":"' + captcha + '"}'}, success: function (data) {
            if (data.success) {
                confirm_Flag = true;
                $("#check_form").addClass('hide');
            } else {
                dialog({type: 'info', align: 'center', message: "校验码不正确，请重新输入"});
                $("#captcha").val("");
                document.getElementById("captcha").focus();
            }
        }});
    }

    function nameIsLegal(name) {
        var cond_1 = /^.*(先生|女士).*$/;
        var cond_2 = /^[\u4E00-\u9FA5]{2,4}$/;
        if (cond_2.test(name)) {
            if (cond_1.test(name)) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    function checkAllNames() {
        var customerName = [];
        var flag = true;
        $('.c1_name').each(function () {
            if (!flag) {
                return;
            }
            var index = this.id.split('_')[1];
            var value = $(this).val();
            if (value.length == 0) {
                fieldVerify($('#c1CustomerName_' + index), '请填写出游人姓名', false);
                flag = false;
            } else if (nameIsLegal(value)) {
                var searchIndex = customerName.indexOf(value);
                if (searchIndex == '-1') {
                    customerName[index] = value;
                    fieldVerify($('#c1CustomerName_' + index));
                } else if (searchIndex == index) {
                } else {
                    fieldVerify($('#c1CustomerName_' + index), '请输入不同的出游人信息', false);
                    flag = false;
                }
            } else {
                fieldVerify($('#c1CustomerName_' + index), '请填写正确的出游人姓名', false);
                flag = false;
            }
        });
        return flag;
    }
});